//
//  MessageListViewModel.m
//  carcareIOS
//
//  Created by ileo on 16/9/5.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MessageListViewModel.h"
#import "BaseWebViewController.h"
#import "WebViewHandle.h"

@interface MessageListViewModel()

@property (nonatomic , strong) UIView * noDataView;

@end
@implementation MessageListViewModel

- (instancetype)initWithMessageCenterModel:(MessageCenterModel *)model
{
    self = [super init];
    if (self) {
        self.type = model.type;
        self.messageModels = model.info;
        self.firstID = model.firstID;
        self.lastID = model.lastID;
        
        __weak __typeof(self) wself = self;
        if (self.type == MessageCenter_Car) {
            RACSignal * modelsValidSignal = RACObserve(self, self.carMessages);
            [modelsValidSignal subscribeNext:^(id x) {
                [((MessageCarListViewController *)wself.viewController).tableView reloadData];
            }];
        }else{
            RACSignal * modelsValidSignal = RACObserve(self, self.messageModels);
            [modelsValidSignal subscribeNext:^(NSArray<MessageModel *>*messageModels) {
                [((MessageListNormalViewController *)wself.viewController).tableView reloadData];
            }];
        }
    }
    return self;
}

-(void)loadMoreSuccess:(void (^)(NSInteger))success fail:(void (^)())fail{
    __weak __typeof(self) wself = self;
    [NetMessageCenter messageLoadWithFirstID:0 lastID:self.lastID messageType:self.type Success:^(NSArray<MessageModel *> *infos) {
        if (infos.count > 0) {
            wself.lastID = infos.lastObject.ID;
            [wself addMessageModels:infos];
        }
        if (success) {
            success(infos.count);
        }
    } fail:^{
        if (fail) {
            fail();
        }
    }];
}

-(void)addMessageModels:(NSArray<MessageModel *> *)models{
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.messageModels];
    [arr addObjectsFromArray:models];
    self.messageModels = [arr copy];
}

#pragma mark - delegate

-(NSInteger)MessageNums
{
    return (self.type != MessageCenter_Car ? self.messageModels.count : self.carMessages.count);
}
-(MessageModel *)messageAtIndex:(NSInteger)index
{
    return self.messageModels[index];
}
-(NSDictionary *)carMessageAtIndex:(NSInteger)index
{
    return self.carMessages[index];
}
-(void)updateSuccess:(void (^)(NSInteger))success fail:(void (^)())fail{
    __weak __typeof(self) wself = self;
    [NetMessageCenter messageLoadWithFirstID:self.firstID lastID:0 messageType:self.type Success:^(NSArray<MessageModel *> *infos) {
        if (infos.count > 0) {
            wself.firstID = infos.firstObject.ID;
            [wself addMessageModels:infos];
        }
        if (success) {
            success(infos.count);
        }
    } fail:^{
        if (fail) {
            fail();
        }
    }];
}
-(MessageCenter_Type)messageType{
    return self.type;
}
-(void)viewDidDisappear{
    if (self.type != MessageCenter_Insurance) {
        [[MessageCenterManager sharedInstance] readMessageType:self.type messageID:self.firstID];
    }
}
-(void)clickOnMessageAtIndex:(NSInteger)index
{
    if (self.type == MessageCenter_Insurance) {
        MessageModel *model = [self.messageModels objectAtIndex:index];
        [[MessageCenterManager sharedInstance] readMessageType:self.type messageID:model.ID];
        model.STATUS = YES;
        [((MessageListNormalViewController *)self.viewController).tableView reloadData];
        if (![Check isVisiter])
        {
            BaseWebViewController * webVC = [[[BaseWebViewController alloc]initWithURL:[WebViewHandle urlAddUserParamsWithURL:model.JUMP_URL]]navSetupBackItemWithIdentification:BACK_ICON];
            [((MessageCenterViewController *)self.viewController).navigationController pushViewController:webVC animated:YES];
        }
    }
}

#pragma mark - set get
-(void)setMessageModels:(NSArray<MessageModel *> *)messageModels{
    _messageModels = [messageModels copy];
    if (self.type == MessageCenter_Car) {
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:messageModels.count];
        __block NSString *dateTime = @"";
        [messageModels enumerateObjectsUsingBlock:^(MessageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            BOOL isFirst = NO;
            if (![dateTime isEqualToString:obj.dateTime]) {
                dateTime = obj.dateTime;
                isFirst = YES;
                [arr addObject:@{keyType:typeDate, keyInfo:dateTime}];
            }
            BOOL isLast = (messageModels.count == idx + 1);
            if (!isLast) {
                isLast = ![[messageModels objectAtIndex:idx + 1].dateTime isEqualToString:dateTime];
            }
            [arr addObject:@{keyType:typeModel, keyInfo:obj, keyIsFirst :@(isFirst), keyIsLast:@(isLast)}];
        }];
        self.carMessages = [arr copy];
    }
}
- (UIView *)noDataView
{
    if (!_noDataView) {
        _noDataView = [UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64)];
        [_noDataView setBackgroundColor:[UIColor whiteColor]];
        // 无行程的图片
        UIImage * noDataImage = ImageNamed(@"messageNoDataIcon");
        UIImageView * noDataImageV = [[UIImageView imageViewWithFrame:CGRectMake(0, 0, noDataImage.size.width * __MAIN_RATIO_375, noDataImage.size.height * __MAIN_RATIO_375) image:noDataImage] setupOnView:_noDataView];
        [noDataImageV setCenter:CGPointMake(_noDataView.center.x, _noDataView.center.y - 70 * __MAIN_RATIO_H_667)];
        //   [Factory imageViewWithCenter:CGPointMake(self.center.x, self.center.y - 70 * __MAIN_RATIO_H_667) image:ImageNamed(@"wayNoDataIcon") onView:self];
        UILabel * tipsL= [Factory labelWithFrame:CGRectMake(0, 0, 200 * __MAIN_RATIO_375, 20 * __MAIN_RATIO_H_667) font:FONT_DEFAULT_Light(18) text:@"暂时没有消息哦～" textColor:COLOR_HEXSTRING(@"949494") onView:_noDataView textAlignment:NSTextAlignmentCenter];
        tipsL.center = CGPointMake(_noDataView.center.x, noDataImageV.bottom + 40 * __MAIN_RATIO_375);
        tipsL.alpha = 0.6;
    }
    return _noDataView;
}
- (UIView *)returnNoDataView
{
    return self.noDataView;
}
- (void)reloadNoDataHidden:(BOOL)hidden
{
    [self.noDataView setHidden:hidden];
}
- (void)clickLabelTextUrl:(NSString *)url
{
    if (![Check isVisiter])
    {
        BaseWebViewController * webVC = [[[BaseWebViewController alloc]initWithURL:[WebViewHandle urlAddUserParamsWithURL:url]]navSetupBackItemWithIdentification:BACK_ICON];
        [((MessageCenterViewController *)self.viewController).navigationController pushViewController:webVC animated:YES];
    }
}
@end
